package com.sllp.C71S3TsllpShop.biz.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.sllp.C71S3TsllpShop.bean.ChartData;
import com.sllp.C71S3TsllpShop.bean.OrdersItem;
import com.sllp.C71S3TsllpShop.bean.Product;
import com.sllp.C71S3TsllpShop.bean.ProductExample;
import com.sllp.C71S3TsllpShop.bean.Specs;
import com.sllp.C71S3TsllpShop.bean.ProductExample.Criteria;
import com.sllp.C71S3TsllpShop.biz.ProductBiz;
import com.sllp.C71S3TsllpShop.dao.ProductMapper;
import com.sllp.C71S3TsllpShop.util.GenerateOrdersItemChartData;

@Service
public class ProductBizImpl implements ProductBiz {

	@Resource
	private ProductMapper pm;
	@Resource
	private GenerateOrdersItemChartData generateOrdersItemChartData;
	
	/**
	 * 根据商铺id查询商品
	 */
	public List<Product> selectByBusiness(int business) {
		ProductExample example = new ProductExample();
		example.createCriteria().andBusinessEqualTo(business);
		List<Product> list = pm.selectByExampleWithBLOBs(example);
		return list;
	}
	/**
	 * 根据店铺id查询商品获得总数量
	 */
	public Long countByExample(int business) {
		ProductExample example = new ProductExample();
		example.createCriteria().andBusinessEqualTo(business);
		long i = pm.countByExample(example);
		return i;
	}
	@Override
	public List<Product> selectByExampleWithBLOBs() {
		List<Product> list = pm.selectByExampleWithBLOBs(null);
		System.out.println(list);
		return list;
	}
	/**
	 * 通过smallTypeid 查找商品
	 * @param id
	 * @return
	 */
	public List<Product> selectBySmallType(int id){
		ProductExample productExample = new ProductExample();
		productExample.createCriteria().andTypeEqualTo(id);
		List<Product> list = pm.selectByExample(productExample);
		return list;
	}
	
	public List<ChartData> generateIndexChartData1(Product name) {
		// 根据商品名查找所有订单
		List<OrdersItem> ordersItems = new ArrayList<OrdersItem>();
		for(Product product : name.getBusiness2().getProducts()) {
			for(Specs specs : product.getSpecs()) {
				if(specs.getOrdersItems().size()>0) {
					ordersItems.addAll(specs.getOrdersItems());
				}
			}
		}
		return generateOrdersItemChartData.generateOrdersItemChartData(ordersItems);
	}
	
	/**
	 * 销售额前八位的商品显示（热销）
	 */
	@Override
	public List<Product> findHotProduct(){
		List<Product> plist= pm.selectByExample(null);
		// List集合排序 排完序后 取前8个放到一个List集合中
		Collections.sort(plist, new Comparator<Product>() {
		    public int compare(Product o1, Product o2) {
		        //按照buynum字段进行降序排列
		        if (o1.getBuyNum() < o2.getBuyNum()) {
		            return 1;
		        }
		        if (o1.getBuyNum() == o2.getBuyNum()) {
		            return 0;
		        }
		        return -1;
		    }
		});
		List<Product> list=plist.subList(0, 8);
		return list;
	}
	
	@Override
	public List<Product> findProductByCondition(Product product, Integer prePrice, Integer nextPrice, Integer page, Integer pageSize){
		ProductExample productExample = new ProductExample();
		Criteria criteria = productExample.createCriteria();
		if(product!=null) {
			if(product.getType()!=null) {
				criteria.andTypeEqualTo(product.getType());
			}
			if(product.getBusiness()!=null) {
				criteria.andBusinessEqualTo(product.getBusiness());
			}
			
		}
		if(prePrice!=null && nextPrice!=null) {
			criteria.andMarketPriceBetween(new BigDecimal(prePrice), new BigDecimal(nextPrice));
		}
		if(page!=null && pageSize != null) {
			PageHelper.startPage(page,pageSize);
			return pm.selectByExampleWithBLOBs(productExample);
		}else {
			return pm.selectByExampleWithBLOBs(productExample);
		}
	}
}
